﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CK.Plugin;
using CK.Kernel.Config;
using System.Collections;
using CommonServices;

namespace GesturePlugin
{
    [PluginEditor(Id = "{E456B7AE-1CA3-4931-92BB-34F189D4FE68}",
        PublicName = "Gesture Editor",
        EditorOf = "{E1FF5C6B-3D8D-4d7f-B4C6-7838AEDFA238}",
        EditableObjectTypes = new string[] { "User" },
        Version = "1.0.0",
        Categories = new string[] { "Editors" })]

    public partial class GestureEditor : IPluginEditor
    {
        public IPluginConfigAccessor EditedConfiguration { get; set; }

        public int DefaultPrecision
        {
            get
            {
                object o = EditedConfiguration.User["DefaultPrecision"];
                return o == null ? 50 : (int)o;
            }

            set { EditedConfiguration.User["DefaultPrecision"] = value; }
        }

        public IList GestureDictionnary
        {
            get
            {
                object o = EditedConfiguration.User["gestureDictionnary"];
                return o == null ? new ArrayList() : (IList)o;
            }
        }

        #region IPlugin Membres

        public bool CanStart(out string lastError)
        {
            throw new NotImplementedException();
        }

        public bool Setup(ISetupInfo info)
        {
            throw new NotImplementedException();
        }

        public void Start()
        {
            throw new NotImplementedException();
        }

        public void Stop()
        {
            throw new NotImplementedException();
        }

        public void Teardown()
        {
            throw new NotImplementedException();
        }

        #endregion

        /// <summary>
        /// Creates the panel
        /// </summary>
        /// <param name="editableObjectTypes"></param>
        /// <param name="o"></param>
        /// <returns>the panel</returns>
        public Panel CreatePanel(string[] editableObjectTypes, object o)
        {
            Panel p = new Panel() { AutoSize = true };
            GesturePluginUserEditor u = new GesturePluginUserEditor(this);

            p.Controls.Add(u);

            return p;
        }

        /// <summary>
        /// Adds a gesture to the dictionnary
        /// </summary>
        /// <param name="gestureName">The key in the dictionnary</param>
        /// <param name="_gestureString">The gesture pattern</param>
        public void AddNewGesture(string gestureName, string _gestureString)
        {
            Hashtable gestureDictionnary = (Hashtable)EditedConfiguration.User["gestureDictionnary"];            

            IList gesturePatterns;
            if (gestureDictionnary.ContainsKey(gestureName))
            {
                gesturePatterns = (IList)gestureDictionnary[gestureName];
                if (!gesturePatterns.Contains(_gestureString))
                {
                    gesturePatterns.Add(_gestureString);
                }
            }
            else
            {
                gesturePatterns = new ArrayList();
                gesturePatterns.Add(_gestureString);
                gestureDictionnary.Add(gestureName, gesturePatterns);
            }

            EditedConfiguration.User["gestureDictionnary"] = gestureDictionnary;
        }
    }
}
